import 'dart:math';

import 'package:flutter/material.dart';

class CircleOpenProgress extends CustomPainter {
  CircleOpenProgress(
    this.raius, {
    this.color,
    this.width,
    this.backgroundColor,
    this.start = 0,
    this.end = 1,
  }) : assert(start < 1 || start < end || end < 1 || end > 0);
  final double raius;
  final Color? backgroundColor;
  final Color? color;
  final double? width;
  final double start;
  final double end;

  @override
  void paint(Canvas canvas, Size size) {
    var circle = 2 * pi;
    var a = circle * 0.25;
    var total = min(1, end - start);
    final endNew = total * circle;
    final startNew = start * circle + a;
    final totalNew = endNew;

    var r = width ?? 10;
    var w = size.width - r / 2;
    var h = size.height - r / 2;
    Offset center = Offset(size.width / 2, size.height / 2);
    double progress = min(totalNew * raius, totalNew);
    var sc = color ?? Colors.blue;
    if (progress > 0) {
      canvas.drawArc(
        Rect.fromCircle(center: center, radius: min(w, h) / 2),
        startNew,
        progress,
        false,
        Paint()
          ..strokeWidth = r
          ..strokeCap = StrokeCap.round
          ..color = sc
          ..style = PaintingStyle.stroke,
      );
    }
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: min(w, h) / 2),
      startNew,
      endNew,
      false,
      Paint()
        ..strokeWidth = r
        ..strokeCap = StrokeCap.round
        ..color = backgroundColor ?? sc.withAlpha(60)
        ..style = PaintingStyle.stroke,
    );
  }

  @override
  bool shouldRepaint(CircleOpenProgress oldDelegate) => false;

  @override
  bool shouldRebuildSemantics(CircleOpenProgress oldDelegate) => false;
}
